package com.zui.webapp.action;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.security.core.context.SecurityContextHolder;

import com.opensymphony.xwork2.ActionSupport;
import com.zui.security.User;
import com.zui.util.Page;
import com.zui.util.UploadedJQFile;

public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	private static final long serialVersionUID = 1L;
	protected transient final Log log = LogFactory.getLog(getClass());
	public static final String JQUERY_FILE_UPLOAD_INPUT_FIELD = "uploadedFile";
	
	/**
	 * HttpServletRequest object
	 */
	protected HttpServletRequest request = null;
	@Override
	public void setServletRequest(HttpServletRequest request) {this.request = request;}
	public HttpServletRequest getRequest() {return request;}

	/**
	 * HttpServletResponse object
	 */
	protected HttpServletResponse response = null;
	@Override
	public void setServletResponse(HttpServletResponse response) {this.response = response;}

	/**
	 * excel steam which is used for excel result
	 */
	protected InputStream excelStream; 
	public void setExcelStream(InputStream excelStream) {this.excelStream = excelStream;}
	public InputStream getExcelStream() {return excelStream;}
	
	/**
	 * selected Menu
	 */
	protected String selectedMenu = "Home";
	public String getSelectedMenu() {	return selectedMenu;}
	public void setSelectedMenu(String selectedMenu) {this.selectedMenu = selectedMenu;}
	
	
	/**
	 * ajax flag for sitemesh decrator.
	 */
	protected boolean ajax = false;
	
	/**
	 * pagination for displaytag
	 */
	protected Page page; 

	/**
	 * file name exported
	 */
	protected String fileNameExported;
	public void setFileNameExported(String fileNameExported) {this.fileNameExported = fileNameExported;}
	public String getFileNameExported() {return fileNameExported;}
	
	/**
	 * single file uploaded
	 */
	protected File uploadedFile;
	protected String uploadedFileFileName;
	protected String uploadedFileContentType;

	protected long fileSize;
	
	/**
	 * multiple file upload
	 */
	protected List<File> uploadedFiles = new ArrayList<File>();
	protected List<String> uploadedFilesContentType = new ArrayList<String>();
	protected List<String> uploadedFilesFileName = new ArrayList<String>();
	
	/**
	 * json result for jquery-file-upload
	 */
	protected List<UploadedJQFile> uploadedJQFiles = new ArrayList<UploadedJQFile>();
	
	/**
	 * 得到当前登陆用户
	 * 
	 * @return
	 */
	public User getCurrentUser() {
		if (SecurityContextHolder.getContext() != null
				&& SecurityContextHolder.getContext().getAuthentication() != null
				&& SecurityContextHolder.getContext().getAuthentication()
						.getPrincipal() != null) {
			Object principal = SecurityContextHolder.getContext()
					.getAuthentication().getPrincipal();
			if (principal instanceof User)
				return ((User) principal);
		}
		return null;
	}

	/**
	 * 得到当前登陆用户的登录用户名
	 * 
	 * @return
	 */
	public String getCurrentUserName() {
		if (getCurrentUser() != null)
			return getCurrentUser().getUserName();
		else
			return null;

	}

	@SuppressWarnings("unchecked")
	/**
	 * the message will be displayed in messages.jsp
	 * @param msg
	 */
	protected void saveMessage(String msg) {
		List<String> messages = (List<String>) request.getSession()
				.getAttribute("messages");
		if (messages == null) {
			messages = new ArrayList<String>();
		}
		messages.add(msg);
		request.getSession().setAttribute("messages", messages);
	}

	/************************************/

	/**
	 * 得到程序地址
	 * 
	 * @param request
	 * @return
	 */
	protected String getAppContextPath() {
		StringBuffer url = new StringBuffer();
		int port = request.getServerPort();
		if (port < 0) {
			port = 80;
		}
		String scheme = request.getScheme();
		url.append(scheme);
		url.append("://");
		url.append(request.getServerName());
		if ((scheme.equals("http") && (port != 80))
				|| (scheme.equals("https") && (port != 443))) {
			url.append(':');
			url.append(port);
		}
		url.append(request.getContextPath());

		return url.toString();
	}
	protected String getRemoteIpAddress() {
		if (request.getHeader("x-forwarded-for") == null) { 
			return request.getRemoteAddr().equals("0:0:0:0:0:0:0:1")?"127.0.0.1":request.getRemoteAddr(); 
		} 
		return request.getHeader("x-forwarded-for"); 
	}
	public File getUploadedFile() {
		return uploadedFile;
	}
	public String getUploadedFileFileName() {
		return uploadedFileFileName;
	}
	public String getUploadedFileContentType() {
		return uploadedFileContentType;
	}
	public long getFileSize() {
		return fileSize;
	}
	public List<File> getUploadedFiles() {
		return uploadedFiles;
	}
	public List<String> getUploadedFilesContentType() {
		return uploadedFilesContentType;
	}
	public List<String> getUploadedFilesFileName() {
		return uploadedFilesFileName;
	}
	public List<UploadedJQFile> getUploadedJQFiles() {
		return uploadedJQFiles;
	}
	public void setUploadedFile(File uploadedFile) {
		this.uploadedFile = uploadedFile;
	}
	public void setUploadedFileFileName(String uploadedFileFileName) {
		this.uploadedFileFileName = uploadedFileFileName;
	}
	public void setUploadedFileContentType(String uploadedFileContentType) {
		this.uploadedFileContentType = uploadedFileContentType;
	}
	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}
	public void setUploadedFiles(List<File> uploadedFiles) {
		this.uploadedFiles = uploadedFiles;
	}
	public void setUploadedFilesContentType(List<String> uploadedFilesContentType) {
		this.uploadedFilesContentType = uploadedFilesContentType;
	}
	public void setUploadedFilesFileName(List<String> uploadedFilesFileName) {
		this.uploadedFilesFileName = uploadedFilesFileName;
	}
	public void setUploadedJQFiles(List<UploadedJQFile> uploadedJQFiles) {
		this.uploadedJQFiles = uploadedJQFiles;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public boolean isAjax() {
		return ajax;
	}
	public void setAjax(boolean ajax) {
		this.ajax = ajax;
	}


}